package com.xzlf.task;

import com.alibaba.fastjson.JSON;
import com.xzlf.domain.JobTask;
import com.xzlf.domain.TFile;
import com.xzlf.mapper.TFileMapper;
import com.xzlf.service.JobTaskService;
import com.xzlf.service.TFileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.scheduling.support.PeriodicTrigger;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Slf4j
@Component
public class DynamicScheduleTask implements SchedulingConfigurer {
    @Autowired
    private TFileMapper tFileMapper;
    @Autowired
    private JobTaskService jobTaskService;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {

        taskRegistrar.addTriggerTask(
                () -> {
                    TFile tFile = tFileMapper.selectById(11);
                    log.info("查询了文件：{}", JSON.toJSONString(tFile));
                },
                triggerContext -> {
                    JobTask jobt = jobTaskService.getById(2);
                    // 定时任务触发，可修改定时任务的执行周期
                    CronTrigger cronTrigger = new CronTrigger(jobt.getJobCron());
                    Date nextExecutionTime = cronTrigger.nextExecutionTime(triggerContext);
                    return nextExecutionTime;
                });
    }
}
